package com.ciwong.epaper.modules.me.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.SchoolDetail;
import com.ciwong.mobilelib.utils.ViewHolder;

import java.util.List;

/**
 * 选择学校Adapter
 *
 * @author bin
 * @version ciwong v.1.0 2015/8/10 17:33
 * @since ciwong v.1.0
 */
public class ChooseSchoolAdapter extends BaseAdapter
{

    /**
     * 上下文对象
     */
    private Context mContext;

    /**
     * 学校信息集
     */
    private List<SchoolDetail> mSchoolDetails;

    /**
     * 选中的学校
     */
    private SchoolDetail mSchoolDetail;

    /**
     * 构造函数
     *
     * @param context
     *            上下文对象
     * @param schoolDetails
     *            学校信息集
     * @param schoolDetail
     *            当前选中的学校信息
     */
    public ChooseSchoolAdapter(Context context,
            List<SchoolDetail> schoolDetails, SchoolDetail schoolDetail)
    {
        this.mContext = context;
        this.mSchoolDetails = schoolDetails;
        this.mSchoolDetail = schoolDetail;
    }

    @Override
    public int getCount()
    {
        return mSchoolDetails != null ? mSchoolDetails.size() : 0;
    }

    @Override
    public Object getItem(int position)
    {
        return mSchoolDetails.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v;
        if (convertView == null)
        {
            v = View.inflate(mContext, R.layout.adapter_choose_school, null);
        }
        else
        {
            v = convertView;
        }
        CheckBox chooseSchoolBox = ViewHolder.get(v, R.id.chooseSchoolBox);
        TextView chooseSchoolTV = ViewHolder.get(v, R.id.chooseSchoolTV);

        SchoolDetail schoolDetail = (SchoolDetail) getItem(position);
        if (mSchoolDetail != null)
        {
            if (mSchoolDetail.equals(schoolDetail))
            {
                chooseSchoolBox.setChecked(true);
            }
            else
            {
                chooseSchoolBox.setChecked(false);
            }
        }
        else
        {
            chooseSchoolBox.setChecked(false);
        }
        chooseSchoolTV.setText(schoolDetail.getSchoolName());
        return v;
    }

    /**
     * 添加学校详情数据
     *
     * @param schoolDetails
     *            学校详情集
     */
    public void addAll(List<SchoolDetail> schoolDetails)
    {
        if (this.mSchoolDetails != null)
        {
            this.mSchoolDetails.addAll(schoolDetails);
            notifyDataSetChanged();
        }
    }

    /**
     * 清空学校详情数据
     */
    public void clear()
    {
        if (this.mSchoolDetails != null && !this.mSchoolDetails.isEmpty())
        {
            this.mSchoolDetails.clear();
            notifyDataSetChanged();
        }
    }

    /**
     * 设置当前选中学校信息
     *
     * @param schoolDetail
     *            学校信息
     */
    public void setSchoolDetail(SchoolDetail schoolDetail)
    {
        this.mSchoolDetail = schoolDetail;
    }

    /**
     *
     * 设置选中项
     *
     * @param schoolDetail
     *            学校详细信息
     */
    public void setCheckedItem(SchoolDetail schoolDetail)
    {
        if (schoolDetail != null && mSchoolDetails != null)
        {
            if (mSchoolDetails.indexOf(schoolDetail) != -1)
            {
                if (this.mSchoolDetail != null
                        && this.mSchoolDetail.equals(schoolDetail))
                {
                    this.mSchoolDetail = null;
                }
                else
                {
                    this.mSchoolDetail = schoolDetail;
                }
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 获取已选中的学校
     *
     * @return 学校详情
     */
    public SchoolDetail getSchoolDetail()
    {
        return mSchoolDetail;
    }
}
